package cn.fengshenghospital.controller;


import cn.fengshenghospital.vo.JsonBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    //退出
    @GetMapping("/exit")
    public JsonBean exit(){
        JsonBean jsonBean = new JsonBean();
        SecurityUtils.getSubject().logout();
        jsonBean.setStatus(200);
        jsonBean.setData("login.html");
        return jsonBean;
    }
    //登录
    @GetMapping("/login")
    public JsonBean login(String userCode, String pwd){
        JsonBean jsonBean = new JsonBean();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userCode,pwd);
        try {
            subject.login(token);
            jsonBean.setStatus(200);
            jsonBean.setData(subject.getPrincipal());
        } catch (Exception e) {
            jsonBean.setStatus(-1);
            jsonBean.setData("账号或密码错误！");
            e.printStackTrace();
        }
        return jsonBean;
    }
}
